package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @description:
 * @program: mytanhua
 * @author: Leon
 * @date: 2021-01-28 20:03
 **/
@Slf4j
@RestController
@RequestMapping("user")
@SuppressWarnings("all")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 完善个人信息-基本信息
     * @param param
     * @param token
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String,String> param, @RequestHeader("Authorization")String token){
//        log.error("@PostMapping(loginReginfo)");
        try{
            Boolean bool = userInfoService.saveUserInfo(param,token);
            if (bool){
                return ResponseEntity.ok(null);
            }
            return null;
        }catch (Exception e){
            ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存信息失败!").build();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
        }
    }

    /**
     * 完善个人信息-头像信息
     * @param file
     * @param token
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto")MultipartFile file, @RequestHeader("Authorization")String token){

        try{
            Boolean bool = userInfoService.saveUserLogo(file,token);
            if (bool){
                return ResponseEntity.ok(null);
            }
            return null;
        }catch (Exception e){
            ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存logo失败!").build();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
        }
    }
}
